package com.zt.location;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.zt.location.modules.socket.server.config.NettyProperties;
import com.zt.location.modules.socket.server.netty.ChannelRepository;
import com.zt.location.modules.socket.server.netty.TCPServer;
import com.zt.location.modules.socket.server.netty.handler.SomethingChannelInitializer;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@EnableSwagger2
@EnableTransactionManagement
@SpringBootApplication
@EnableConfigurationProperties(NettyProperties.class) 
@EnableScheduling
public class Application extends SpringBootServletInitializer {

	 @Autowired
	 private NettyProperties nettyProperties;
	 
	public static void main(String[] args) throws Exception{
		ConfigurableApplicationContext context =SpringApplication.run(Application.class, args);
		TCPServer tcpServer = context.getBean(TCPServer.class);
	    tcpServer.start();
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}
	
	 @Bean(name = "serverBootstrap")
	    public ServerBootstrap bootstrap() {
	        ServerBootstrap b = new ServerBootstrap();
	        b.group(bossGroup(), workerGroup())
	                .channel(NioServerSocketChannel.class)
	                .handler(new LoggingHandler(LogLevel.DEBUG))
	                .childHandler(somethingChannelInitializer);
	        Map<ChannelOption<?>, Object> tcpChannelOptions = tcpChannelOptions();
	        Set<ChannelOption<?>> keySet = tcpChannelOptions.keySet();
	        for (@SuppressWarnings("rawtypes") ChannelOption option : keySet) {
	            b.option(option, tcpChannelOptions.get(option));
	        }
	        return b;
	    }

	    @Autowired
	    private SomethingChannelInitializer somethingChannelInitializer;

	    @Bean
	    public Map<ChannelOption<?>, Object> tcpChannelOptions() {
	        Map<ChannelOption<?>, Object> options = new HashMap<ChannelOption<?>, Object>();
	        options.put(ChannelOption.SO_BACKLOG, nettyProperties.getBacklog());
	        return options;
	    }

	    @Bean(destroyMethod = "shutdownGracefully")
	    public NioEventLoopGroup bossGroup() {
	        return new NioEventLoopGroup(nettyProperties.getBossCount());
	    }

	    @Bean(destroyMethod = "shutdownGracefully")
	    public NioEventLoopGroup workerGroup() {
	        return new NioEventLoopGroup(nettyProperties.getWorkerCount());
	    }

	    @Bean
	    public InetSocketAddress tcpSocketAddress() {
	        return new InetSocketAddress(nettyProperties.getTcpPort());
	    }

	    @Bean
	    public ChannelRepository channelRepository() {
	        return new ChannelRepository();
	    }
}
